package com.shenyue.nbtownlet.treeview;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.util.DensityUtil;

import me.texy.treeview.TreeNode;
import me.texy.treeview.base.CheckableNodeViewBinder;

/**
 * 树节点
 */

public class LevelNodeViewBinder extends CheckableNodeViewBinder {

    private TextView textView;
    private ImageView imageView;
    private int diff = 16;

    public LevelNodeViewBinder(int level, View view) {
        super(view);
        LinearLayout nodeContainer = (LinearLayout) view.findViewById(R.id.node_container);
        if (nodeContainer != null) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) nodeContainer.getLayoutParams();
            layoutParams.setMargins(DensityUtil.dip2px(view.getContext(), (level + 1) * diff), 0, 0, 0);
            nodeContainer.setLayoutParams(layoutParams);
        }

        textView = (TextView) view.findViewById(R.id.node_name_view);
        imageView = (ImageView) view.findViewById(R.id.arrow_img);
    }

    @Override
    public int getCheckableViewId() {
        return R.id.checkBox;
    }

    @Override
    public int getLayoutId() {
        return R.layout.tree_view_item;
    }

    @Override
    public void bindView(final TreeNode treeNode) {
        textView.setText(treeNode.getValue().toString());
        imageView.setRotation(treeNode.isExpanded() ? 90 : 0);
    }

    @Override
    public void onNodeToggled(TreeNode treeNode, boolean expand) {
        if (expand) {
            imageView.animate().rotation(90).setDuration(200).start();
        } else {
            imageView.animate().rotation(0).setDuration(200).start();
        }
    }
}
